﻿/**
 *
 * 版权：C.c 版权所有
 *
 * 作者：C.c
 *
 * 版本：1.0
 *
 * 创建日期：2017/11/02
 *
 * 描述：UI基类
 *
 * 修订历史：
 *
 */

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace XHFrame
{
    /// <summary>
    /// 基础UI
    /// </summary>
    public abstract class BaseUI : MonoBehaviour
    {

        #region (属性) 缓存 GameObject & Transform

        /// <summary>
        /// 缓存游戏对象
        /// </summary>
        private GameObject cacheGameObjcet;

        /// <summary>
        /// 缓存GameObject
        /// </summary>
        public GameObject CacheGameObjcet
        {
            get
            {
                if (cacheGameObjcet == null)
                {
                    cacheGameObjcet = this.gameObject;
                }
                return cacheGameObjcet;
            }
        }


        Transform cacheTransform;

        /// <summary>
        /// 缓存 Transform
        /// </summary>
        public Transform CacheTransform
        {
            get
            {
                if (cacheTransform == null)
                {
                    cacheTransform = this.transform;
                }
                return cacheTransform;
            }
        }
        #endregion


        #region StateObjectState & UIType ( UI状态和UI类型)

        /// <summary>
        /// UI对象类型
        /// </summary>
        private EnumObjectState state = EnumObjectState.None;

        /// <summary>
        /// 当UI状态改变时
        /// </summary>
        public event StateChangedEvent OnStateChanged;


        /// <summary>
        /// UI状态
        /// </summary>
        public EnumObjectState State
        {
            protected get
            {
                return state;
            }

            set
            {
                EnumObjectState oldState = state; // 保存之前的UI类型
                state = value;
                if (OnStateChanged != null)
                {
                    OnStateChanged(this, state, oldState);
                }
            }
        }

        /// <summary>
        /// 返回UI类型
        /// </summary>
        /// <returns></returns>
        public abstract EnumUIType GetUIType();

        #endregion
        // Use this for initialization
        void Start()
        {
            OnStart();
        }

        private void Awake()
        {
            State = EnumObjectState.Initial;
            OnAwake();
        }

        // Update is called once per frame
        void Update()
        {
            if (this.State != EnumObjectState.Ready)
            {
                OnUpdate(Time.deltaTime);
            }
        }



        protected virtual void OnAwake()
        {
            State = EnumObjectState.Loading;
            // 打开声音
            OnPlayOpenUIAudio();
        }

        protected virtual void OnStart() { }
        protected virtual void OnUpdate(float deltaTime) { }

        /// <summary>
        /// 释放UI对象
        /// </summary>
        void Release()
        {
            State = EnumObjectState.Closing;
            Destroy(cacheGameObjcet);
            OnRelease();
        }

        private void OnDestroy()
        {
            State = EnumObjectState.None;
        }

        /// <summary>
        /// 释放
        /// </summary>
        public virtual void OnRelease()
        {
            State = EnumObjectState.None;
            // 关闭声音
            OnPlayCloseUIAudio();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        protected virtual void OnLoadData() { }


        /// <summary>
        /// 打开音乐
        /// </summary>
        protected virtual void OnPlayOpenUIAudio() { }

        /// <summary>
        /// 关闭音乐
        /// </summary>
        protected virtual void OnPlayCloseUIAudio() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UIparams"></param>
        public void SetUIWhenOpening(params object[] UIparams)
        {
            SetUI(UIparams);
            CoroutineController.Instance.StartCoroutine(LoadDataAsyn());
        }

        public virtual void SetUIParam(params object[] uiParams)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UIparams"></param>
        protected virtual void SetUI(params object[] UIparams)
        {
            State = EnumObjectState.Loading;
        }

        /// <summary>
        /// 异步加载改变状态
        /// </summary>
        /// <returns></returns>
        private IEnumerator LoadDataAsyn()
        {
            yield return new WaitForSeconds(0);
            if (State == EnumObjectState.Loading)
            {
                OnLoadData();
                State = EnumObjectState.Ready;
            }
        }
    }

}

